package com.example.javagd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.javagd.pojo.MyTemplatePojo;
import com.example.javagd.service.IMyTemplateService;
import com.example.javagd.util.CommonUtils;
import com.example.javagd.util.Result;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @program: java-general-development
 * @description:
 * @author: hzy
 * @create: 2021-04-02 13:21
 **/
@Controller
@RequestMapping("/template")
@Slf4j
public class MyTemplateController extends CommonUtils {
    private String prefix = this.getClass().getAnnotation(RequestMapping.class).value()[0];

    @Resource(name = "myTemplateServiceImpl")
    IMyTemplateService iMyTemplateService;

    /***************************
     * 在这里添加
     * 业务代码
     * 可以调用已下的数据库接口
     * @return
     */









    /*********************************/
    @ApiOperation(value = "模板: get方法返回page")
    @GetMapping("/getTemplatePage")
    public Object GetTemplatePage(){
        return prefix + "/templatePage";
    }

    @ApiOperation(value = "模板: get方法返回object")
    @GetMapping("/getTemplateObject")
    @ResponseBody
    public Result<Map> GetTemplateObject(){
        Map<String, Object> map = new HashMap<String, Object>(){
            {
                put("key", "value");
            }
        };
        log.info(map.toString());
        return result.Success(map);
    }

    @ApiOperation(value = "模板: post方法返回object")
    @PostMapping("/postTemplateObject")
    @ResponseBody
    public Result<Map> PostTemplateObject(){
        Map<String, Object> map = new HashMap<String, Object>(){
            {
                put("key", "value");
            }
        };
        return result.Success(map);
    }


    @ApiOperation(value = "模板: 新增")
    @PostMapping("/postTemplateObject/add")
    @ResponseBody
    public Result<Object> PostTemplateObjectAdd(@RequestBody MyTemplatePojo myTemplatePojo){
        return iMyTemplateService.Add(myTemplatePojo) ? result.SuccessMsg("add success") : result.ErrorMsg("add fail");
    }

    @ApiOperation(value = "模板: 删除")
    @PostMapping("/postTemplateObject/del")
    @ResponseBody
    public Result<Object> PostTemplateObjectDel(@RequestParam(value="id") Long id){
        return iMyTemplateService.DelById(id) ? result.SuccessMsg("del success") : result.ErrorMsg("del fail");
    }

    @ApiOperation(value = "模板: 查询")
    @PostMapping("/postTemplateObject/select")
    @ResponseBody
    public Result<List<MyTemplatePojo>> PostTemplateObjectSelect(){
        return result.Success(iMyTemplateService.SelectAll());
    }

    @ApiOperation(value = "模板: id查询")
    @PostMapping("/postTemplateObject/selectByid")
    @ResponseBody
    public Result<MyTemplatePojo> PostTemplateObjectSelectById(@RequestParam(value="id") Long id){
        return result.Success(iMyTemplateService.SelectById(id));
    }

    @ApiOperation(value = "模板: map查询")
    @PostMapping("/postTemplateObject/selectByMap")
    @ResponseBody
    public Result<List<MyTemplatePojo>> PostTemplateObjectSelectByMap(){
        return result.Success(iMyTemplateService.SelectByMap(new HashMap<String, Object>(){
            {

            }
        }));
    }

    @ApiOperation(value = "模板: wrapper查询")
    @PostMapping("/postTemplateObject/selectByWrapper")
    @ResponseBody
    public Result<List<MyTemplatePojo>> PostTemplateObjectSelectByWrapper(){
        QueryWrapper<MyTemplatePojo> queryWrapper = new QueryWrapper<>();
        return result.Success(iMyTemplateService.SelectByWrapper(queryWrapper));
    }

    @ApiOperation(value = "模板: map删除")
    @PostMapping("/postTemplateObject/delByMap")
    @ResponseBody
    public Result<List<MyTemplatePojo>> PostTemplateObjectDelByMap(){
        return result.Success(iMyTemplateService.DelByMap(new HashMap<String, Object>(){
            {

            }
        }));
    }

    @ApiOperation(value = "模板: wrapper删除")
    @PostMapping("/postTemplateObject/delByWrapper")
    @ResponseBody
    public Result<List<MyTemplatePojo>> PostTemplateObjectDelByWrapper(){
        QueryWrapper<MyTemplatePojo> queryWrapper = new QueryWrapper<>();
        return result.Success(iMyTemplateService.DelByWrapper(queryWrapper));
    }


    @ApiOperation(value = "模板: wrapper更新")
    @PostMapping("/postTemplateObject/updateByWrapper")
    @ResponseBody
    public Result<List<MyTemplatePojo>> PostTemplateObjectUpdateByWrapper(@RequestBody MyTemplatePojo myTemplatePojo){
        QueryWrapper<MyTemplatePojo> queryWrapper = new QueryWrapper<>();
        return result.Success(iMyTemplateService.UpdateByWrapper(
                myTemplatePojo,
                queryWrapper)
        );
    }


    @ApiOperation(value = "模板: 更新")
    @PostMapping("/postTemplateObject/update")
    @ResponseBody
    public Result<Object> PostTemplateObjectUpdate(@RequestBody MyTemplatePojo myTemplatePojo){
        return iMyTemplateService.Update(myTemplatePojo) ? result.SuccessMsg("update success") : result.ErrorMsg("update fail");
    }

}
